//
//  AppDelegate.m
//  phmall
//
//  Created by admin on 2017/9/5.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "AppDelegate.h"
#import "TabBarVController.h"
static NSString * const kClientID = @"994985096023-2caq5tusbidcfkmsrdjgjgt44st0nnnp.apps.googleusercontent.com";
@interface AppDelegate ()
@property(strong,nonatomic)Reachability* hostReach;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
 
    if (@available(iOS 11.0, *)){//避免滚动视图顶部出现20的空白以及push或者pop的时候页面有一个上移或者下移的异常动画的问题
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(reachabilityChanged:)
     
                                                 name: kReachabilityChangedNotification
     
                                               object: nil];
    
    _hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];//可以以多种形式初始化
    
    [_hostReach startNotifier];  //开始监听,会启动一个run loop
    
    //    ViewController *loginController=[[ViewController alloc]init];
    //    UINavigationController  *nav=[[UINavigationController alloc]initWithRootViewController:loginController];
    //    self.window.rootViewController=nav;
    
    TabBarVController *tab=[TabBarVController new];
    self.mainController = tab;
    self.window.backgroundColor=[UIColor whiteColor]; 
    [self.window makeKeyAndVisible];
    
    self.window.rootViewController=tab;
    
    [self.window makeKeyAndVisible];

    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
    
    keyboardManager.enable = YES; // 控制整个功能是否启用
    
    keyboardManager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
    
    keyboardManager.shouldToolbarUsesTextFieldTintColor = YES; // 控制键盘上的工具条文字颜色是否用户自定义
    
        keyboardManager.toolbarManageBehaviour = IQAutoToolbarBySubviews; // 有多个输入框时，可以通过点击Toolbar 上的“前一个”“后一个”按钮来实现移动到不同的输入框
    
      [IQKeyboardManager sharedManager].enableAutoToolbar = YES; // 控制是否显示键盘上的工具条
    
    //    keyboardManager.shouldShowToolbarPlaceholder = YES; // 是否显示占位文字
    
    keyboardManager.placeholderFont = [UIFont boldSystemFontOfSize:14]; // 设置占位文字的字体
    
    keyboardManager.keyboardDistanceFromTextField = 10.0f; // 输入框距离键盘的距离
    
//    //注册登录状态监听
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(loginStateChange:)
//                                                 name:KNOTIFICATION_LOGINCHANGE
//                                               object:nil];
//
    
    
//    [self easemobApplication:application
//didFinishLaunchingWithOptions:launchOptions
//                      appkey:@"1129170426178834#phchat"
//                apnsCertName:@"phchat"
//                 otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];
//
//    [self.window makeKeyAndVisible];
//
//    EMOptions *options = [EMOptions optionsWithAppkey:@"1129170426178834#phchat"];
//    options.apnsCertName = @"phchat";
//    options.enableDeliveryAck=YES;
//    [[EMClient sharedClient] initializeSDKWithOptions:options];
//
//

    //facebook
    [[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];
    [FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
    [FBSDKSettings setAppID:@"709821129223343"];

    [GIDSignIn sharedInstance].clientID = kClientID;
    
    
    //缓存当前语言
    if (![[NSUserDefaults standardUserDefaults]objectForKey:@"appLanguage"]) {
        
        NSArray *languages = [NSLocale preferredLanguages];
        NSString *language = [languages objectAtIndex:0];
        
        if ([language hasPrefix:@"zh-Hans"]) {
            //开头匹配
            [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"appLanguage"];
        }else{
            [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"appLanguage"];
        }
    }
    
    return YES;
}


-(BOOL)application:(UIApplication*)application openURL:(NSURL*)url options:(NSDictionary<NSString*,id>*)options{
    if (@available(iOS 9.0, *)) {
        BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                      openURL:url
                                                            sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                                   annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
                        ];
        // Add any custom logic here.
        return handled;
    }else{
        return YES;
    }
}  
//- (void)easemobApplication:(UIApplication *)application
//didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//                    appkey:(NSString *)appkey
//              apnsCertName:(NSString *)apnsCertName
//               otherConfig:(NSDictionary *)otherConfig
//{
//
//
//    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
//
//}


- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler{
    return YES;
}
// APP进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//    [[EMClient sharedClient] applicationDidEnterBackground:application];
}

// APP将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application
{
//    [[EMClient sharedClient] applicationWillEnterForeground:application];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}




- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
//MARK: 处理网络监听
// 连接改变

- (void)reachabilityChanged: (NSNotification* )note

{
    
    Reachability* curReach = [note object];
    
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    
    [self updateInterfaceWithReachability: curReach];
    
}


//处理连接改变后的情况

- (void)updateInterfaceWithReachability: (Reachability*) curReach

{
    NSLog(@"网络状况发生改变");
    //对连接改变做出响应的处理动作。
    
    _statue = [curReach currentReachabilityStatus];
    
}

////环信好友回调
//#pragma mark - Deprecated methods
//
///*!
// *  \~chinese
// *  用户B同意用户A的加好友请求后，用户A会收到这个回调
// *
// *  @param aUsername   用户B
// */
//- (void)friendRequestDidApproveByUser:(NSString *)aUsername{
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@同意了你的好友请求",aUsername] message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    [alert show];
//}
///*!
// *  \~chinese
// *  用户B拒绝用户A的加好友请求后，用户A会收到这个回调
// *
// *  @param aUsername   用户B
// */
//- (void)friendRequestDidDeclineByUser:(NSString *)aUsername{
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@拒绝了你的好友请求",aUsername] message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    [alert show];
//}
///*!
// *  \~chinese
// *  用户B删除与用户A的好友关系后，用户A，B会收到这个回调
// *
// *  @param aUsername   用户B
// */
//- (void)friendshipDidRemoveByUser:(NSString *)aUsername{
//
//}
///*!
// *  \~chinese
// *  用户B删除与用户A的好友关系后，用户A会收到这个回调
// *
// *  @param aUsername   用户B
// */
//- (void)didReceiveDeletedFromUsername:(NSString *)aUsername{
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"你删除了%@的好友",aUsername] message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    [alert show];
//}
///*!
// *  \~chinese
// *  用户B同意用户A的好友申请后，用户A和用户B都会收到这个回调
// *
// *  @param aUsername   用户好友关系的另一方
// */
//- (void)friendshipDidAddByUser:(NSString *)aUsername{
//
//}
//
///*!
// *  \~chinese
// *  用户B申请加A为好友后，用户A会收到这个回调
// *
// *  @param aUsername   用户B
// *  @param aMessage    好友邀请信息
// */
//- (void)friendRequestDidReceiveFromUser:(NSString *)aUsername
//                                message:(NSString *)aMessage{
//    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:aUsername,@"username",aMessage,@"message", nil];
//
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//
//    [userDefaults setObject:dic forKey:@"dic"];
//
//    [userDefaults synchronize];
//
//    NSString *title = [NSString stringWithFormat:@"来自%@的好友申请",aUsername];
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:aMessage delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    [alert show];
//}



//facebok
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSDKAppEvents activateApp];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}

@end

